package com.le.tester.javaThink.fifth.chapter24_ConcurrentProgramming;

import java.util.concurrent.CompletableFuture;

/**
 * createTime：2021/8/20 17:05
 * description：DualCompletableOperations
 */
public class DualCompletableOperations {
    static CompletableFuture<Workable> cfA, cfB;

    static void init() {
        cfA = Workable.make("A", 0.15);
        cfB = Workable.make("B", 0.10);
    }

    static void join() {
        cfA.join();
        cfB.join();
        System.out.println("****************");
    }

    public static void main(String[] args) {
        init();
        CompletableUtilities.voidr(cfA.runAfterEitherAsync(cfB, () -> System.out.println("runAfterEither")));
        join();

        init();
        CompletableUtilities.voidr(cfA.runAfterBothAsync(cfB, () -> System.out.println("runAfterBoth")));
        join();

        init();
        CompletableUtilities.showr(cfA.applyToEitherAsync(cfB, w -> {
            System.out.println("applyToEitherAsync");
            return w;
        }));
        join();

        init();
        CompletableUtilities.voidr(cfA.acceptEitherAsync(cfB, w -> {
            System.out.println("acceptEitherAsync" + w);
        }));
        join();

        init();
        CompletableUtilities.voidr(cfA.thenAcceptBothAsync(cfB, (w1, w2) -> {
            System.out.println("thenAcceptBothAsync");
        }));
        join();

        init();
        CompletableUtilities.showr(cfA.thenCombineAsync(cfB, (w1, w2) -> {
            System.out.println("thenCombineAsync" + w1 + "," + w2);
            return w1;
        }));
        join();

        init();
        CompletableFuture<Workable> cfC = Workable.make("C", 0.08),
                cfD = Workable.make("D", 0.09);

        CompletableFuture.anyOf(cfA, cfB, cfC, cfD).thenRunAsync(() -> System.out.println("anyOf"));
        join();

        init();
        cfC = Workable.make("C", 0.08);
        cfD = Workable.make("D", 0.09);

        CompletableFuture.allOf(cfA, cfB, cfC, cfD)
                .thenRunAsync(() -> System.out.println("allOf"));
        join();
    }
}
